/**
 * @author shenw
 */
(function(window){
	var root = pack('jshen.dom');
	// jshen dom object
	function Dom(expr, dom) {
		return this.find(expr, dom);
	};
	Dom.prototype = Array.prototype;
	Dom.method('hide', function(){
		for (var i = 0; i < this.length; i++) {
			this[i].oddDisplay = this[i].style.display;
			this[i].style.display = "none";
		}
		return this;
	});
	Dom.method('show', function(){
		for (var i=0; i < this.length; i++) {
			if (this[i].oddDisplay && this[i].oddDisplay !== 'none') {
				this[i].style.display = this[i].oddDisplay;
				this[i].oddDisplay = undefined;
			} else {
				this[i].style.oddDisplay = this[i].style.display;
				this[i].style.display = "block";
			}
		}
		return this;
	});
	Dom.method('layerCenter', function(){
		this.layerCenterH();
		this.layerCenterV();
		return this;
	});
	Dom.method('layerCenterH', function(){
		var lca, lcb, lcx;
		var iebody;
		var dsocleft;
		for (var i=0; i<this.length; i++) {
			if (window.innerWidth) {
				lca = window.innerWidth;
			} else {
				lca = document.body.clientWidth;
			}
			lcb = this[i].offsetWidth;
			lcx = Math.round((lca - lcb) / 2);
			iebody = (document.compatMode
				&& document.compatMode !== 'BackCompat')
				? document.documentElement
				: document.body;
			dsocleft = document.all ? iebody.scrollLeft : window.pageXOffset;
			this[i].style.left = lcx + dsocleft + 'px';
		}
		return this;
	});
	Dom.method('layerCenterV', function(){
		var lca, lcb, lcy;
		var iebody;
		var dsoctop;
		for (var i=0; i<this.length; i++) {
			if (window.innerHeight) {
				lca = window.innerHeight;
			} else {
				lca = document.body.clientHeight;
			}
			lcb = this[i].offsetHeight;
			lcy = Math.round((lca - lcb) / 2);
			iebody = (document.compatMode
				&& document.compatMode !== 'BackCompat')
				? document.documentElement
				: document.body;
			dsoctop = document.all ? iebody.scrollTop : window.pageYOffset;
			this[i].style.top = lcy + dsoctop + 'px';
		}
		return this;
	});
	Dom.method('css', function(name, value){
		for (var i = 0; i<this.length; i++) {
			if (typeof value === 'undefined') {
				// get value
				return this[i].style[name];
			} else if (value === null) {
				// remove css
				this[i].style[name] = undefined;
			} else {
				// set value
				this[i].style[name] = value;
			}
		}
		return this;
	});
	Dom.method('find', function(expr, dom){
	    if (dom == null) {//null,undefined
	        dom = document;
	    }
	    regex = /^([#.]?)(\w+)/;
	    var group = regex.exec(expr);
	    if (group !== null) {
	        switch (group[1]) {
	            case '#':
					this.push(dom.getElementById(group[2]));
	            case '.':
					var list = dom.getElementsByClassName(group[2]);
					for (var i=0; i<list.length; i++) {
						this.push(list[i]);
					}
	            case '':
					var list = dom.getElementsByTagName(group[2]);
					for (var i=0; i<list.length; i++) {
						this.push(list[i]);
					}
	        }
	    }
	    return this;
	});
	
	root.method('find', function(expr, dom){
		return new Dom(expr, dom);
	});
	
	window._ = root.find;
})(window);
